// express  插件
// 别人开发的一个插件
const { readFile, writeFile } = require("fs");
const { join } = require("path");

const books = join(__dirname, "books.json");
//作用:这个插件(模块) 搭建服务器
const express = require("express");
const app = express();
const port = 3000;

// app.get("/", (req, res) => {
//   res.send("Hello Girl!");
// });

app.get("/api/getbooks", (req, res) => {
  //req - request  -请求
  //res - response -响应
  readFile(books, "utf-8", (err, data) => {
    if (err) {
      res.send({ status: 1, message: "获取失败" });
    } else {
      res.send({ status: 0, message: "获取成功", data: JSON.parse(data) });
    }
  });
  res.send();
});

app.post("/api/addbooks", (req, res) => {
  req.body.id = Date.now();
  readFile(file, "utf-8", (err, data) => {
    if (err) throw err;
    let err = JSON.parse();
  });
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});
